# -*- coding: utf-8 -*-

import pandas as pd
from typing import List


class AliyunLog:
    log_level: str
    log_topic: str
    log_time: str
    message: str


class AliyunLogParser:
    file_path: str
    logs: List[AliyunLog]

    def __init__(self, file_path: str):
        self.file_path = file_path
        self.logs = []

        self._load_file()

    def _load_file(self):
        if self.file_path is None:
            raise BaseException('Aliyun log file not found')

        df = pd.read_csv(self.file_path)
        for i in df.index.values:
            aliyun_log = AliyunLog()

            # 旗舰版和钉版日志格式不同
            lower_file_path = self.file_path.lower()
            if 'ng-' in lower_file_path:
                aliyun_log.log_level = df.iloc[i, 4]
                aliyun_log.message = df.iloc[i, 8]
                aliyun_log.log_time = df.iloc[i, 9]
                aliyun_log.log_topic = df.iloc[i, 11]
            elif 'dt-' in lower_file_path:
                aliyun_log.log_level = df.iloc[i, 7]
                aliyun_log.message = df.iloc[i, 12]
                aliyun_log.log_time = df.iloc[i, 5]
                aliyun_log.log_topic = df.iloc[i, 0]
            else:
                raise BaseException('Aliyun log file name not valid, must starts with ng- or dt-')

            self.logs.append(aliyun_log)
